//
//  DCFMacro.h
//  DCF
//
//  Created by 吴文拼 on 2017/12/25.
//  Copyright © 2017年 吴文拼. All rights reserved.
//
#import "UIColor+HexColors.h"
#import "DCFCommonUtils.h"
#import "UIView+DCFToast.h"

#ifndef DCFMacro_h
#define DCFMacro_h

//借条名字
#define IOU_NAME(str) [str stringByReplacingOccurrencesOfString:@"借条" withString:[DCFCtrlViewManager shareInstance].showTrust?@"借条":@"电子合同"]
#define IOU_VIEW_SHOW_SWITCH [DCFCtrlViewManager shareInstance].showTrust//

//appId
#define APP_STORE_ID @"1344696410"

#define WEAKSELF   __typeof(&*self) __weak weakSelf = self;
#define STRONGSELF __typeof(&*self) __strong strongSelf = weakSelf;

// UI相关
#define DEFAULT_IMAGE_COLOR_IPHONE  0xdedede

//是否为iphoneX
#define kDevice_Is_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)

// 屏幕方向 (在IOS 6之前并且在window makeKeyAndVisible那个runloop内无效)
#define CURRENT_ORIENTATION                 [[UIApplication sharedApplication] statusBarOrientation]
#define CURRENT_ORIENTATION_IS_LANDSCAPE    UIInterfaceOrientationIsLandscape(CURRENT_ORIENTATION)
#define CURRENT_ORIENTATION_IS_PORTRAIT     UIInterfaceOrientationIsPortrait(CURRENT_ORIENTATION)

// 屏幕尺寸
#define SCREEN_BOUNS         [UIScreen mainScreen].bounds
#define SCREEN_SIZE         [UIScreen mainScreen].bounds.size
#define SCREEN_WIDTH        SCREEN_SIZE.width
#define SCREEN_HEIGHT       SCREEN_SIZE.height
#define SCREEN_CONTENT_HEIGHT (SCREEN_HEIGHT - 64)
#define SCREEN_SCALE        [UIScreen mainScreen].scale
#define kScreenScale     SCREEN_WIDTH / 375.0
#define SCREEN_ORIENTATION_WIDTH    MAX(SCREEN_HEIGHT, SCREEN_WIDTH)
#define SCREEN_ORIENTATION_HEIGHT   MIN(SCREEN_HEIGHT, SCREEN_WIDTH)

#define IS_IPHONE           (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5         (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
#define IS_IPAD             (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_4INCH_SCREEN (SCREEN_HEIGHT == 568.f)

#define IS_RETINA_SCREEN    [[UIDevice currentDevice] isRetinaScreen]

// 状态栏尺寸
#define STATUS_BAR_SIZE     [UIApplication sharedApplication].statusBarFrame.size
#define STATUS_BAR_WIDTH    MAX(STATUS_BAR_SIZE.height, STATUS_BAR_SIZE.width)
#define STATUS_BAR_HEIGHT   MIN(STATUS_BAR_SIZE.height, STATUS_BAR_SIZE.width)

#define STATUS_NAVIGATIONBAR_HEIGHT (STATUS_BAR_HEIGHT+44.0f)

#define TABBAR_HEIGHT (kDevice_Is_iPhoneX?83.0f:49.0f)

#define STATUS_NAVIGATIONBAR_TABBAR_HEIGHT (STATUS_NAVIGATIONBAR_HEIGHT+TABBAR_HEIGHT)

//角度转换
#define degreeToRadian(degree) ((degree * (float)M_PI) / 180.0f)

// 开始绘制阴影O
#define SHADOW_START_WITH_PARAMS(context, uicolor, width, height, blur)    \
CGContextSaveGState(context);\
CGContextSetShadowWithColor(context, CGSizeMake(width, height), blur, uicolor.CGColor);\

// 结束阴影绘制
#define SHADOW_STOP_WITH_PARAMS(context)     \
CGContextRestoreGState(context);

// 文字宽高
#define TextWidth(ak_font,ak_rect,ak_content) [DCFCommonUtils widthWithFont:ak_font maxHeight:CGRectGetHeight(ak_rect) string:ak_content]
#define TextHeight(ak_font,ak_width,ak_content) [DCFCommonUtils heightWithFont:ak_font maxWidth:ak_width string:ak_content]

// RGB颜色
#define RGBCOLOR(r,g,b)     [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define RGBACOLOR(r,g,b,a)  [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
#define HEXRGBCOLOR(h)      RGBCOLOR(((h>>16)&0xFF), ((h>>8)&0xFF), (h&0xFF))
#define HEXRGBACOLOR(h,a)   RGBACOLOR(((h>>16)&0xFF), ((h>>8)&0xFF), (h&0xFF), a)

#define CLEARCOLOR          [UIColor clearColor]
#define IOS7NavColor        [UIColor colorWithRed:174/255.0 green:6/255.0 blue:15/255.0 alpha:1]

// Device相关

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

#define IS_iOS8 ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0f&&[[UIDevice currentDevice].systemVersion floatValue] < 9.0f)

// 是否是iOS 7或者更高版本
#define IS_IOS7_OR_HIGHER       SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")

// 是否是iOS 8或者更高版本
#define IS_IOS8_OR_HIGHER       SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")

#define IS_IOS10_OR_HIGHER       SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Math
#define FLOAT_EQUAL(f1, f2)     ((fabs(f1-f2) < FLT_EPSILON) ? YES : NO)

////////////////////////////////////////////////////////////////////////////////
// SDK 版本兼容
#ifdef __IPHONE_7_0

#define UITextAlignment       NSTextAlignment
#define UITextAlignmentLeft   NSTextAlignmentLeft
#define UITextAlignmentCenter NSTextAlignmentCenter
#define UITextAlignmentRight  NSTextAlignmentRight

#define UILineBreakMode                       NSLineBreakMode
#define UILineBreakModeWordWrap               NSLineBreakByWordWrapping
#define UILineBreakModeCharacterWrap          NSLineBreakByCharWrapping
#define UILineBreakModeClip                   NSLineBreakByClipping
#define UILineBreakModeHeadTruncation         NSLineBreakByTruncatingHead
#define UILineBreakModeTailTruncation         NSLineBreakByTruncatingTail
#define UILineBreakModeMiddleTruncation       NSLineBreakByTruncatingMiddle

#endif

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
#define kCGImageAlphaPremultipliedFirst  (kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst)
#define kCGImageAlphaPremultipliedLast   (kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast)
#else
#define kCGImageAlphaPremultipliedFirst  kCGImageAlphaPremultipliedFirst
#define kCGImageAlphaPremultipliedLast   kCGImageAlphaPremultipliedLast
#endif

////////////////////////////////////////////////////////////////////////////////
// iOS 7 界面适配
#ifdef __IPHONE_7_0
#define ADJUST_VIEW_FOR_IOS7(viewController, view) \
if ([viewController respondsToSelector:@selector(edgesForExtendedLayout)]){ \
UIRectEdge rectEdge = viewController.edgesForExtendedLayout; \
if (rectEdge & UIRectEdgeTop){ \
float top = [[UIApplication sharedApplication] statusBarFrame].size.height + viewController.navigationController.navigationBar.frame.size.height; \
CGRect frame = view.frame; \
frame.origin.y += top; \
view.frame = frame; \
} \
}
#else
#define ADJUST_VIEW_FOR_IOS7(viewController, view)
#endif

#ifdef __IPHONE_7_0
#define ADJUST_SCROLLVIEW_FOR_IOS7(viewController, scrollView) \
if ([viewController respondsToSelector:@selector(edgesForExtendedLayout)]){ \
UIRectEdge rectEdge = viewController.edgesForExtendedLayout; \
if (rectEdge & UIRectEdgeTop){ \
float top = [[UIApplication sharedApplication] statusBarFrame].size.height + viewController.navigationController.navigationBar.frame.size.height; \
scrollView.contentInset = UIEdgeInsetsMake(scrollView.contentInset.top+top, scrollView.contentInset.left, scrollView.contentInset.bottom, scrollView.contentInset.right); \
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(scrollView.scrollIndicatorInsets.top+top, \
scrollView.scrollIndicatorInsets.left, \
scrollView.scrollIndicatorInsets.bottom, \
scrollView.scrollIndicatorInsets.right); \
} \
}
#else
#define ADJUST_SCROLLVIEW_FOR_IOS7(viewController, scrollView)
#endif

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// 设计模式

// 单例模式: SINGLETON_GENERATOR(ActionEngine, sharedInstance)
#define SINGLETON_GENERATOR(class_name, shared_func_name)    \
static class_name * s_##class_name = nil;                       \
+ (class_name *)shared_func_name                                \
{                                                               \
static dispatch_once_t once;                                \
dispatch_once(&once, ^{                                     \
s_##class_name = [[super allocWithZone:NULL] init];     \
});                                                         \
return s_##class_name;                                      \
}                                                               \
+ (class_name *)allocWithZone:(NSZone *)zone                    \
{                                                               \
return s_##class_name;                                      \
}                                                               \
- (class_name *)copyWithZone:(NSZone *)zone                     \
{                                                               \
return self;                                                \
}                                                              \

// 配合TM_SINGLETON_GENERATOR 返回静态实例
#define TM_SINGLETON_SHARED_INSTANCE(class_name)    s_##class_name

// 用在init中检测是否已经init过
#define TM_SINGLETON_CHECK_INITED(class_name)                   \
{if (TM_SINGLETON_SHARED_INSTANCE(class_name)) return TM_SINGLETON_SHARED_INSTANCE(class_name);}





#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
#define BS_TEXTSIZE(text, font) [text length] > 0 ? [text \
sizeWithAttributes:@{NSFontAttributeName:font}] : CGSizeZero;
#else
#define BS_TEXTSIZE(text, font) [text length] > 0 ? [text sizeWithFont:font] : CGSizeZero;
#endif



#define PATH_OF_APP_HOME    NSHomeDirectory()
#define PATH_OF_TEMP        NSTemporaryDirectory()
#define PATH_OF_DOCUMENT    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
#define PATH_OF_LIBRARY     [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject]
#define PATH_OF_CACHE       [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

#ifdef DEBUG
#define NSLog(...) NSLog(__VA_ARGS__)
#else
#define NSLog(...)
#endif


//VC
#define VIEW_CONTROLLER_TITLE_HEIGHT        44
#define VIEW_CONTROLLER_TITLE_MAXW          160
#define VIEW_CONTROLLER_NAVGBAR_HEIGHT      (VIEW_CONTROLLER_TITLE_HEIGHT + 4)
#define VIEW_CONTROLLER_TOOLBAR_HEIGHT      (IS_IOS7_OR_HIGHER ? 49 : 44)
#define VIEW_CONTROLLER_CONTENT_HEIGHT      (APPLICATION_HEIGHT - VIEW_CONTROLLER_TITLE_HEIGHT - VIEW_CONTROLLER_TOOLBAR_HEIGHT)
#define HEADER_BTN_W                        50
#define HEADER_BTN_H                        44
#define HEADER_LEFTBTN_W                    40
#define HEADER_LEFTBTN_H                    32
#define HEADER_LEFTBTN_INSET_LEFT           14
#define HEADER_LEFTBTN_INSET_RIGHT          8
#define HEADER_BTN_LEFT_MARGIN              8
#define HEADER_BTN_TOP_MARGIN               ((VIEW_CONTROLLER_TITLE_HEIGHT - HEADER_BTN_H) / 2)
#define TITLELABEL_X                        (HEADER_LEFTBTN_W + HEADER_BTN_LEFT_MARGIN * 2)
#define TITLE_BUTTON_FONTSIZE               16
#define TITLE_BAR_FONTSIZE                  18
#define HEADER_BTN_IMG_INSET_LEFT           (-16)



//一天的秒数
#define SECONDS_PER_DAY (60.0 * 60.0 * 24)

//主色
#define kMainColorHexStr @"108EE9"
#define kMainColor [UIColor colorWithHexString:kMainColorHexStr]

//设置fontsize
#define GetFontWithSize(s) [UIFont systemFontOfSize:s]

#define FontThin(fSize)            [UIFont fontWithName:@"PingFangSC-Thin" size:fSize] ?: GetFontWithSize(fSize)
#define FontLight(fSize)           [UIFont fontWithName:@"PingFangSC-Light" size:fSize] ?: GetFontWithSize(fSize)
#define FontMedium(fSize)          [UIFont fontWithName:@"PingFangSC-Medium" size:fSize] ?: GetFontWithSize(fSize)
#define FontRegular(fSize)         [UIFont fontWithName:@"PingFangSC-Regular" size:fSize] ?: GetFontWithSize(fSize)
#define FontBlodRegular(fSize)         [UIFont fontWithName:@"DINAIternate-Blodcenter" size:FontSizeScale(fSize)] ?: GetFontWithSize(fSize)



//用户反馈的appkey和appscret
static NSString * const kYWFeedBackAppKey = @"";
static NSString * const kYWFeedBackAppSecret = @"";





#pragma mark ---- NSNotificationCenter ----
//消息中心
//App唤醒
#define DCF_APP_BECOME_ACTIVE @"DCF_app_become_active"
//App进入后台
#define DCF_APP_ENTER_BACKGROUND @"DCF_app_enter_background"
//用户登录通知
#define DCF_NOTIFY_USER_LOGIN @"DCF_notify_user_login"
//用户注册完成通知
#define DCF_NOTIFY_USER_REGISTER_END @"DCF_notify_user_register_end"
//用户登出通知
#define DCF_NOTIFY_USER_LOGOUT @"DCF_notify_user_logout"
//用户401重新登录通知
#define DCF_NOTIFY_USER_401_SHOULD_RELOGIN @"DCF_notify_user_401_should_relogin"
//用户信息修改后刷新用户信息展示
#define DCF_NOTIFY_REFRESH_USER_INFO @"DCF_notify_refresh_user_info"


#pragma mark ---- NSUserDefaults ----
#define DCFUserDefaults [NSUserDefaults standardUserDefaults]


//判断xxxx是否为空(支持类型NSString、NSArray、NSDictionary、NSSet)
#define IS_BLANK(obj) [DCFCommonUtils isEmpty:obj]

#endif /* DCFMacro_h */
